package org.hcm.compile.main;

import java.awt.*;
import java.io.IOException;

import org.hcm.compile.lexing.LexAnalyse;
import org.hcm.compile.parsing.Parser;
import org.hcm.compile.utils.FileInput;

import javax.swing.*;

/**
 * @author HCM
 */
public class ocmpile {
    static JFrame jf;
    static JPanel jpanel;
    static JTextArea jta = null;
    static JScrollPane jscrollPane;


    public void create() {
        jf = new JFrame("");
        Container contentPane = jf.getContentPane();
        contentPane.setLayout(new BorderLayout());
        jta = new JTextArea(100, 150);
        jta.setTabSize(4);
        jta.setFont(new Font("标楷体", Font.BOLD, 16));
        jta.setLineWrap(true);// 激活自动换行功能
        jta.setWrapStyleWord(true);// 激活断行不断字功能
//        jta.setBackground();
        jscrollPane = new JScrollPane(jta);
        jpanel = new JPanel();
        jpanel.setLayout(new GridLayout(1, 3));
        contentPane.add(jscrollPane, BorderLayout.CENTER);
        contentPane.add(jpanel, BorderLayout.SOUTH);
        jf.setSize(1000, 1000);
        jf.setLocation(400, 20);
        jf.setVisible(true);
    }

    public static void main(String[] args) throws IOException {


        ocmpile ocmpile = new ocmpile();
        ocmpile.create();
        String fileName = "D:\\java_project\\u\\src\\test00.c";
//		ocmpile ocmpile=new ocmpile();
//		ocmpile.lexAnalyse(fileName,jta);
        if (ocmpile.parser(fileName, jta)) {
//			fourElem(fileName,jta);
        } else {
            System.out.println("\n语法分析出错，不能生成四元式");
        }
    }

    /**
     * 词法分析
     *
     * @param fileName
     * @throws IOException
     */
    public void lexAnalyse(String fileName, JTextArea jta) throws IOException {
        LexAnalyse lex = new LexAnalyse();
        lex.lexAnalyse1(fileName);
        System.out.println("输出信息到文件：" + lex.outputWordList(jta) + "\n");
    }

    /**
     * 语法分析
     *
     * @param fileName
     * @throws IOException
     */
    public boolean parser(String fileName, JTextArea jt) throws IOException {
        create();
        Parser parser = new Parser(new LexAnalyse(FileInput.readFile(fileName)));
        parser.grammerAnalyse(jta);
        System.out.println("输出信息到文件：" + parser.outputLL1(jt) + "\n");
        return true;
    }

    /**
     * 生成四元式
     *
     * @param fileName
     * @throws IOException
     */
    public void fourElem(String fileName, JTextArea jt) throws IOException {
        create();
        Parser parser = new Parser(new LexAnalyse(FileInput.readFile(fileName)));
        parser.grammerAnalyse(jta);
        System.out.println("输出信息到文件：" + parser.outputFourElem(jta));
    }

}
